<?php
defined('IN_TS') or die('Access Denied.');
/* 
 * 小组管理
 */	

switch($ts){

	//小组列表
	case "list":
		$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
		$url = 'index.php?app=group&ac=admin&mg=group&ts=list&page=';
		$lstart = $page*10-10;
		$arrGroup = $DB->fetch_all_assoc("select * from ".dbprefix."app_group order by addtime desc limit $lstart,10");
		$groupNum = $DB->once_num_rows("select * from ".dbprefix."app_group");
		if(is_array($arrGroup)){
			foreach($arrGroup as $key=>$item){
				$arrAllGroup[] = $item;
				$arrAllGroup[$key]['groupdesc'] = getsubstrutf8($item['groupdesc'],0,40);
			}
		}
		$pageUrl = pagination($groupNum, 10, $page, $url);

		include TmooTemplate("admin/group_list");
		
		break;
	
	//小组添加
	case "add":
		include TmooTemplate("admin/group_add");
		break;
	
	//小组添加执行
	case "add_do":
		$userid = trim($_POST['userid']);
		$strUser = $DB->once_fetch_assoc("select * from ".dbprefix."app_user_info where userid='$userid'");
		$arrData = array(
			'userid' => $userid,
			'groupname'	=> trim($_POST['groupname']),
			'groupdesc'	=> trim($_POST['groupdesc']),
			'isrecommend'	=> trim($_POST['isrecommend']),
			'addtime'	=> time(),
			'ispost'	=> $_POST['ispost'],
		);
		$groupid = $DB->insertArr($arrData,'app_group');
		//更新group_users索引关系
		$groupUserNum = $DB->once_num_rows("select * from ".dbprefix."app_group_users where userid='$userid' and groupid='$groupid'");
		if($groupUserNum > 0){
		}else{
			//插入小组成员索引
			$DB->query("insert into ".dbprefix."app_group_users (`userid`,`groupid`) values ('".$userid."','".$groupid."')");
			//计算小组会员数
			$groupUserNum = $DB->once_num_rows("select * from ".dbprefix."app_group_users where groupid='$groupid'");
			//更新小组成员统计
			$DB->query("update ".dbprefix."app_group set `count_user`='$groupUserNum' where groupid='$groupid'");
		}
		//回到小组管理首页
		header("Location: index.php?app=group&ac=admin&mg=group&ts=list");
		break;
	
	//小组编辑
	case "edit":
		$groupid = $_GET['groupid'];
		$arrGroup = $DB->once_fetch_assoc("select * from ".dbprefix."app_group where groupid='$groupid'");
		include TmooTemplate("admin/group_edit");
		break;
	
	//小组编辑执行
	case "edit_do":
		$groupid = $_POST['groupid'];
		$arrData = array(
			'groupname'		=> $_POST['groupname'],
			'groupdesc'		=> $_POST['groupdesc'],
			'isrecommend'		=> $_POST['isrecommend'],
			'userid'			=> $_POST['userid'],
			'ispost'	=> $_POST['ispost'],
			'isshow'	=> $_POST['isshow'],
			'isaudit'	=> $_POST['isaudit'],
		);
		$DB->updateArr($arrData,'app_group','where groupid='.$groupid.'');
		header("Location: index.php?app=group&ac=admin&mg=group&ts=edit&groupid=".$groupid."");
		break;
	
	//小组删除
	case "del":
		$groupid = $_POST['groupid'];
		
		$DB->query("DELETE FROM ".dbprefix."app_group WHERE groupid = '$groupid'");
		$DB->query("DELETE FROM ".dbprefix."app_group_cates_index WHERE groupid = '$groupid'");
		$DB->query("DELETE FROM ".dbprefix."app_group_users WHERE groupid = '$groupid'");
		
		echo '0';
		break;
}